package com.hss.cn

import android.content.ContentValues
import org.litepal.LitePal
import org.litepal.LitePal.where
import org.litepal.extension.max
import org.litepal.extension.min
import org.litepal.extension.sum
import java.util.*


object ShootHelper {

    /**
     * 保存某条记录(存在则更新)
     * @return
     */
    fun saveMsg(data: ShootData) {
        data.date = Date().toString()
        data.hitDate = UtilTime.getCurrentTime()
        data.hitRate = (UtilMath.round(UtilMath.div(data.hitNbr.toDouble(), data.sum.toDouble()), 2) * 100f)
        data.saveOrUpdate("hitDate = ?", data.hitDate)
    }

    /**
     * 获取今日数据
     */
    fun getTodayData(): MutableList<ShootData> {
        var data: MutableList<ShootData> = where("hitDate = ?", UtilTime.getCurrentTime()).find(ShootData::class.java)
        return data
    }

    /**
     * 获取最佳命中率
     */
    fun getBestRate(): Int {
        val result = where("sum >= ?", "50").max<Int>("ShootData", "hitRate")
        return result
    }

    /**
     * 获取最差命中率
     */
    fun getWorstRate(): Int {
        val result = where("sum >= ?", "50").min<Int>("ShootData", "hitRate")
        return result
    }

    /**
     * 获取命中总数
     */
    fun getHitSum(): Int {
        val result = LitePal.where("sum >= ?", "50").sum<ShootData, Int>("hitNbr")
        return result
    }

    /**
     * 获取投篮总数
     */
    fun getSum(): Int {
        val result = where("sum >= ?", "50").sum<ShootData, Int>("sum")
        return result
    }

    /**
     * 获取平均命中率
     */
    fun getAverage(): String {
        val result = where("sum >= ?", "50").average(ShootData::class.java, "hitRate")
        return result.toString()
    }

    /**
     * 清空今日数据
     * @param loginMsg
     */
    fun clearTodayData() {
        val values = ContentValues()
        values.put("sum", "0")
        values.put("hitNbr", "0")
        values.put("loseNbr", "0")
        values.put("hitRate", "0")
        LitePal.updateAll(ShootData::class.java, values, "hitDate = ?", UtilTime.getCurrentTime())
    }
}